﻿using System;
using System.Web.UI.WebControls;
using DBProvider;
using DBProvider.Providers;

namespace HealthCommunity.Controls
{
	public partial class UCMyCourse : System.Web.UI.UserControl
	{
		private ICourseProvider _courseProvider;

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				BindCourse();
			}
		}

		protected void gvCourse_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if(e.Row.RowType == DataControlRowType.DataRow)
			{
				var hlCourse = e.Row.FindControl("hlCourse") as HyperLink;

				if(hlCourse != null)
				{
					var dataItem = e.Row.DataItem as Course;

					hlCourse.NavigateUrl = "~/CourseDetail.aspx?courseid=" + dataItem.CourseID.ToString();
				}
			}
		}

		private void BindCourse()
		{
			if(SiteContext.Current != null && SiteContext.Current.CurrentUser != null)
			{
				if(_courseProvider == null)
				{
					_courseProvider = new CourseProvider();
				}

				gvCourse.DataSource = _courseProvider.GetCourseByUser(SiteContext.Current.CurrentUser.UserID);
				gvCourse.DataBind();
			}
		}
	}
}